package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.RegionQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(){
        return "region/list";
    }

    @RequestMapping("/query")
    @ResponseBody
    public List getRegionByParentId(RegionQueryObject qo,String type){
        List<Region> list = regionService.getRegionByParentId(qo);
        if ("tree".equals(type)) {
            List<Map> map = new ArrayList<>();
            for (Region region : list) {
                map.add(region.getMap());
            }
            return map;
        }
        return list;

    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        regionService.saveOrUpdate(region);
        return new JSONResult();
    }


    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Long id,Integer state){
        regionService.changeState(id,state);
        return new JSONResult();
    }

    @RequestMapping("/allRegions")
    @ResponseBody
    public List<Region> queryAllRegions(){
        List<Region> regions=regionService.queryAllRegions();
        return regions;
    }
}
